package cn.com.xf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.common.StringUtil;
import cn.com.xf.dao.TbareainfoDAO;
import cn.com.xf.dao.TbchinaareaDAO;
import cn.com.xf.dao.TblDestinationDAO;
import cn.com.xf.dao.TsSiteinfoDAO;
import cn.com.xf.domain.Tbchinaarea;
import cn.com.xf.domain.TblDestination;
import cn.com.xf.domain.TsSiteinfo;
import cn.com.xf.service.DestinService;
import cn.com.xf.utils.CheckChinaArea;
import cn.com.xf.web.form.DestinForm;

@Service
public class DestinSeriveImpl implements DestinService{
	
	@Autowired
	private TblDestinationDAO destinDAO;
	
	@Autowired
	private TbchinaareaDAO chinareaDAO;
	
	@Autowired
	private TbareainfoDAO areainfoDAO;
	
	@Autowired
	private TsSiteinfoDAO siteinfoDAO;
	
	
	public int countCodeid(String codeid){
		int count = destinDAO.countCodeid(codeid);
		 return count;
	}
	
	public TblDestination selectById(Integer id) {		
		return destinDAO.selectById(id);
	}

	
	public String deleteById(Integer id) {
		String result = null;
		TblDestination coreDest = destinDAO.selectById(id);
		String status = coreDest.getStatus();
		if(status.equals("0")){
			destinDAO.deleteById(id);
			result = "0";
		}else if(status.equals("1")){
			result = "1";
		}		
		return result;
	}
	public Map<String,Object> insert(DestinForm dest){
		String result = null;
		Map<String,Object> map = new HashMap<String, Object>();
		TblDestination coreDest = new TblDestination();
		String prov = StringUtil.StringFilter(dest.getProv());
		String city = StringUtil.StringFilter(dest.getCity());
		String area = StringUtil.StringFilter(dest.getArea());
		String address = dest.getAddress().replaceAll("\\s*", "");
		String areaname = dest.getAreaname().replaceAll("\\s*", "");
		String codeid = SHA1CODEID(prov, city, area, address);
		String fldofsites = areainfoDAO.selectByAreaname(areaname);
		if(destinDAO.countCodeid(codeid) > 0){
			result = "0";
			TblDestination td = destinDAO.selectCodeid(codeid);
			TsSiteinfo ts = siteinfoDAO.selectByPrimaryKey(td.getSiteid());
			map.put("sitename", ts.getSitename());
			map.put("siteid", td.getSiteid());
			map.put("areaname", td.getAreaname());
			map.put("resu", result);
			return map;
		}else if(fldofsites == null || fldofsites.equals("")){
			result = "-2";
			map.put("resu", result);
			return map;
		}else{
			coreDest.setSiteid(dest.getSiteid());
			coreDest.setUserid(dest.getUserid());
			coreDest.setProv(prov);
			coreDest.setCity(city);
			coreDest.setArea(area);
			coreDest.setAddress(address);
			coreDest.setAreaname(areaname);
			coreDest.setMemotext(dest.getMemotext());
			coreDest.setUpdateTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			coreDest.setAddTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			coreDest.setStatus("0");
			coreDest.setCodeid(codeid);
			coreDest.setFldofsites(fldofsites);
			try {
				destinDAO.insert(coreDest);
				result = "1";
				map.put("resu", result);
				return map;
			} catch (Exception e) {
				result = "-1";
				map.put("resu", result);
				return map;
			}
		}
		
	}
	/**
	 * return 返回0为储存失败或数库有此目的地，返回1为储存成功
	 */
	public Map<String,Object> insertCheck(DestinForm dest) {
		String result = null;
		Map<String,Object> map = new HashMap<String, Object>();
		TblDestination coreDest = new TblDestination();
		String inProv = StringUtil.StringFilter(dest.getProv());
		String inCity = StringUtil.StringFilter(dest.getCity());
		String inArea = StringUtil.StringFilter(dest.getArea());
		String inAddress = dest.getAddress().replaceAll("\\s*", "");
		String areaname = dest.getAreaname().replaceAll("\\s*", "");
		//省市相同，把县（区）替换成市
		if(inProv.equals(inCity)){
			inCity = inArea;
			inArea = "";
		}
		String address = inAddress.replaceAll(dest.getProv(), "").replaceAll(dest.getCity(), "").replaceAll(dest.getArea(), "");
		String prov = CheckChinaArea.checkProv(inProv);
		List<Tbchinaarea> tbcCity = chinareaDAO.ProvAndCity(prov);
		String[] excludeCityArr = {"台湾省","香港特别行政区","澳门特别行政区"};		
		String city = CheckChinaArea.checkCity(inCity, tbcCity);
		for (int i = 0; i < excludeCityArr.length; i++) {
			if(!excludeCityArr[i].equals(prov)) {
				if(city.equals("") || prov.equals("")) {
					result = "-1";
					map.put("resu", result);
					return map;
				}
			}
		}
		List<Tbchinaarea> tbcArea = chinareaDAO.CityAndArea(city);
		String area = CheckChinaArea.checkArea(inArea, tbcArea);
		String codeid = SHA1CODEID(prov, city, area, address);
		String fldofsites = areainfoDAO.selectByAreaname(areaname);		
		if(destinDAO.countCodeid(codeid) > 0){
			result = "0";
			TblDestination td = destinDAO.selectCodeid(codeid);
			TsSiteinfo ts = siteinfoDAO.selectByPrimaryKey(td.getSiteid());
			map.put("sitename", ts.getSitename());
			map.put("siteid", td.getSiteid());
			map.put("areaname", td.getAreaname());
			map.put("resu", result);
			return map;
		}else if(fldofsites == null || fldofsites.equals("")){
			result = "-2";
			map.put("resu", result);
			return map;
		}else{
			coreDest.setSiteid(dest.getSiteid());
			coreDest.setUserid(dest.getUserid());
			coreDest.setProv(prov);
			coreDest.setCity(city);
			coreDest.setArea(area);
			coreDest.setAddress(address);
			coreDest.setAreaname(areaname);
			coreDest.setMemotext(dest.getMemotext());
			coreDest.setUpdateTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			coreDest.setAddTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			coreDest.setStatus("0");
			coreDest.setCodeid(codeid);
			coreDest.setFldofsites(fldofsites);
			try {
				destinDAO.insert(coreDest);
				result = "1";
				map.put("resu", result);
				return map;
			} catch (Exception e) {
				result = "-1";
				map.put("resu", result);
				return map;
			}
		}
		
	}

	
	public Map<String,Object> updateById(DestinForm dest) {
		Integer id = dest.getId();
		String result = "0";
		Map<String,Object> map = new HashMap<String, Object>();
		TblDestination selectDest = destinDAO.selectById(id);
		String status = selectDest.getStatus();
		if(status.equals("0")){
			TblDestination coreDest = new TblDestination();
			String prov = dest.getProv().replaceAll("\\s* ", "");
			String city = dest.getCity().replaceAll("\\s*", "");
			String area = dest.getArea().replaceAll("\\s*", "");
			String address = dest.getAddress().replaceAll("\\s*", "");
			String areaname = dest.getAreaname().replaceAll("\\s*", "");
			String codeid = SHA1CODEID(prov, city, area, address);
			TblDestination checkDest = destinDAO.selectCodeid(codeid);
			String fldofsites = areainfoDAO.selectByAreaname(areaname);
			if(fldofsites == null || fldofsites.equals("")){
				result = "-1";
				map.put("resu", result);
				return map;
			}else if(checkDest == null || checkDest.getId().equals(id)){
				coreDest.setId(dest.getId());
				coreDest.setUserid(dest.getUserid());
				coreDest.setProv(prov);
				coreDest.setCity(city);
				coreDest.setArea(area);
				coreDest.setAddress(address);
				coreDest.setAreaname(areaname);
				coreDest.setMemotext(dest.getMemotext());
				coreDest.setUpdateTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
				coreDest.setCodeid(codeid);
				coreDest.setFldofsites(fldofsites);
				try {
					destinDAO.updateById(coreDest);
					result = "1";
					map.put("resu", result);
				} catch (Exception e) {
					result = "0";
					map.put("resu", result);
				}				
				return map;
			}
		}
		return map;
		
	}

	
	public int updateByStatus(DestinForm dest) {
		TblDestination coreDest = new TblDestination();
		coreDest.setId(dest.getId());
		coreDest.setAuditor(dest.getAuditor());
		coreDest.setStatus(dest.getStatus());
		coreDest.setCheckTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		int rows = destinDAO.updateByStatus(coreDest);
		return rows;
	}

	


	
	public int selectCount(String city,String area,String address,String areaname,String siteid,String status) {		
		return destinDAO.selectCount(city, area, address, areaname, siteid,status);
	}


	@Override
	public List<TblDestination> selectDestin(int begin, int pageSize, String orderByField, String orderByMode,
			String city, String area, String address, String areaname, String siteid ,String status) {		
		return destinDAO.selectDestin(begin,pageSize,orderByField,orderByMode,city,area,address,areaname,siteid,status);
	}


	private static String SHA1CODEID(String prov,String city,String area,String address){		
		String destStr = prov+city+area+address;		
		String sha1code = DigestUtils.sha1Hex(destStr).toUpperCase();
		return sha1code;
		
	}

	@Override
	public int updateByAllStatus(DestinForm dest) {
		TblDestination coreDest = new TblDestination();
		coreDest.setSiteid(dest.getSiteid());
		coreDest.setAuditor(dest.getAuditor());
		coreDest.setStatus(dest.getStatus());
		coreDest.setCheckTimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		int rows = destinDAO.updateByAllStatus(coreDest);
		return rows;
	}
	

}
